<template>
  <Modal v-model="modal" :title="title" @on-cancel="onCancel">
    <div class="tag-modal">
      <Form :label-width="80" :model="form" ref="form" :rules="rules">
        <FormItem label="月份:" prop="yearmonth">
          <DatePicker type="month" :options="options3" placeholder="请输入月份" style="width: 200px" v-model="form.yearmonth"></DatePicker>
        </FormItem>
      </Form>
    </div>
    <div slot="footer">
      <Button @click="cancel" size="large" type="text">取消</Button>
      <Button :disabled="btnDisabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
  </Modal>
</template>
<script>
import { formatMonth } from '@/util'
export default {
  data() {
    return {
      modal: false,
      title: '导出',
      btnDisabled: false,
      form: {
        yearmonth: ''
      },
      rules: {
        yearmonth: [
          {
            required: true,
            message: '请选择月份',
            type: 'date',
            trigger: 'change'
          }
        ]
      },
      options3: {
        disabledDate (date) {
          // return date && date.valueOf() > Date.now() - 24 * 60 * 60 * 1000 * 30
          var dateNow = new Date()
          return date && date.valueOf() > dateNow.getTime()
        }
      }
    }
  },
  methods: {
    openModal() {
      this.modal = true
    },
    onCancel() {
      this.modal = false
    },
    cancel() {
      this.modal = false
    },
    onSave() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.btnDisabled) return
          this.btnDisabled = true
          let param = {
            yearmonth: (formatMonth(this.form.yearmonth)).replace('-','')
          }
          let url = '/salesman/identify_report/export'
          this.$jsonPost(url, param).then(data => {
            this.$Modal.success({
              title: '导出成功',
              content: '请到首页-导出列表处下载',
              onOk: () => {
                this.$router.push('/home/export/list')
              }
            })
            this.$emit('complete')
            this.onCancel()
            this.btnDisabled = false
          }).catch(() => {
            this.btnDisabled = false
          })
        }
      })
    }
  }
}
</script>